define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, D_add, $window, $timeout, $ionicSlideBoxDelegate, GoPage, GoDialog, $stateParams, $ionicPopup, GoAddress, $ionicModal, GoPicture) {
		$scope.selectTab = 0;
		$scope.scope = new Object();
		$scope.scope.id1 = null;
		$scope.scope.id2 = null;
		$scope.scope.id3 = null;
		$scope.act_id = $stateParams.act_id;
		$scope.scope.brand = "0";
		$scope.cat = new Array();
		$scope.price = new Array();
		var cat_type = new Array();
		var price_type = new Array();
		$scope.cat_types = new Array();
		$scope.price_types = new Array();
		var rule = new Array();
		$scope.scope.flg = false;
		$scope.type = 0;
		var goods_info = new Array();
		var region_info = new Array();
		var store_info = new Array();
		$scope.scope.flg = false;
		var x = 0;
		var store = new Array();
		$scope.store = new Array();
		var goods = new Array();
		$scope.goods = new Array();
		$scope.choose_store = new Array();
		$scope.input = new Object();
		$scope.cat_type = new Array();
		$scope.input.key = null;
		$scope.input.province = null; //省市区三级联动
		$scope.input.city = null;
		$scope.input.district = null;
		$scope.input.region = null;
		$scope.input.cat_type = null;
		$scope.input.cat1 = null;
		$scope.input.cat2 = null;
		$scope.input.cat3 = null;
		$scope.input.name = null;
		$scope.input.brand_id = null;
		$scope.input.agent_count = 0;
		$scope.input.goods_count = 0;
		$scope.is_same = false;
		$scope.goods_is_same = false;
		$scope.province_name = null;
		$scope.city_name = null;
		$scope.districe_name = null;
		$scope.region_name = null;
		$scope.scope.image = new Array();
		$scope.scope.create_group = false;
		$scope.scope.has_packets = false;
		$scope.scope.cat_type = new Array();
		$scope.datas = new Object();
		$scope.cat_id = null;
		$scope.cat_name = null;
		$scope.v_type = 1;
		$scope.p_type = 0;
		$scope.cat_itme1 = new Array();
		$scope.cat_itme2 = new Array();
		$scope.activeTab = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		$scope.slideChanged = function(index) {
			$scope.selectTab = index;
		};

		$scope.isLoad = false; //可以加载
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			GoPage.init_top($scope, 78, 0);
			$scope.init_data();
		}, false);
		//滑动时候触发
		$scope.selectIndex = 0;
		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.selectIndex = index;
		};
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
		};
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.refreshComplete');
			}, 1000);
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			}, 1000);
		};

		$scope.video_type = function(type) {
			if(type == 'live') {
				$scope.scope.recrod = false;
				$scope.scope.none = false;
				$scope.v_type = 1;
			} else if(type == 'record') {
				$scope.scope.live = false;
				$scope.scope.none = false;
				$scope.v_type = 2;
			} else if(type == 'none') {
				$scope.scope.live = false;
				$scope.scope.recrod = false;
				$scope.v_type = 3;

			}
		}
		//图片移除
		$scope.ondelete = function(index) {
			$scope.scope.image.splice(index, 1);
		};
		//动态响应
		$scope.doShowFile = function() {
			document.getElementById("upfilebutton").click();
		}
		//图片上传
		$scope.onupload = function(val) {
			var fileList = val.files;
			for(var i = 0; i < val.files.length; i++) {
				var reader = new FileReader();
				reader.readAsDataURL(fileList[i]);
				var file = fileList[i];
				reader.onload = function(e) {
					//图片进行压缩处理
					GoPicture.compress(file,e.target.result, 50, function(base64) {
						$scope.$apply(function() {
							var i = $scope.scope.image.length;
							$scope.scope.image[i] = {
								"id": null,
								"image": base64
							}
							val.value = "";
						});
					});
				}
			}
			//上传成功
			val.value = "";
		};
		//满减、折扣
		$scope.dz = [true, false];
		$scope.dzChanged = function(index) {
			for(var i = 0; i < $scope.dz.length; i++)
				$scope.dz[i] = (i == index);
		}
		//默认
		$scope.scope.discount1 = true;
		$scope.order_type = 1;
		$scope.type = 1;
		$scope.scope.order_type1 = true;
		$scope.discount_type = function(type) {
			if(type == 'discount1') {
				$scope.scope.discount2 = false;
				$scope.type = 1;
			} else { //scope.discount2用来控制隐藏域
				$scope.scope.discount1 = false;
				$scope.type = 2;
			}
		}
		$scope.discount_order_type = function(type) {
			if(type == 'order_type1') {
				$scope.scope.order_type2 = false;
				$scope.order_type = 1;
			} else { //scope.discount2用来控制隐藏域
				$scope.scope.order_type1 = false;
				$scope.order_type = 2;
			}
		}

		$scope.brand_list = new Array();

		$scope.init_data = function() {
			$scope.total = null;
			$scope.price = null;
			$scope.percent = null;
			$scope.cat_types = cat_type;
			$scope.bind('input.province', 'input.city', 'input.distric');
			if($scope.act_id != null) {
				D_add.promotion_detail($scope.act_id, function(data) {
					$scope.scope = data;
					if($scope.scope.video_type == 1) {
						$scope.scope.live = true;
						$scope.scope.recrod = false;
						$scope.scope.none = false;
					} else if($scope.scope.video_type == 2) {
						$scope.scope.live = false;
						$scope.scope.recrod = true;
						$scope.scope.none = false;
					} else if($scope.scope.video_type == 3) {
						$scope.scope.live = false;
						$scope.scope.recrod = false;
						$scope.scope.none = true;
					}
				})
				D_add.promotion_agenter($scope.act_id, function(data) {
					$scope.datas = data;
					for(var i = 0; i < $scope.datas.store.length; i++) {
						$scope.datas.store[i].province = null;
						$scope.datas.store[i].city = null;
						$scope.datas.store[i].district = null;
						//初始化想要绑定的模型
						$scope.bind('datas.store[' + i + '].province', 'datas.store[' + i + '].city', 'datas.store[' + i + '].district', i);
					}
				})
			}
			//品牌
			D_add.brand_list(function(data) {
				$scope.brand_list = data.brand;
			})
		}

		//独立每个城市跟城区的itmes
		$scope.listen_province = function(data, id) {
			//动态刷新items，并且防止删除时的回调报错
			if(id != undefined&&$scope.datas.store.length>id) {
				$scope.datas.store[id].city_items = data;
				$scope.datas.store[id].district_items = null;
				$scope.datas.store[id].city = null;
				$scope.datas.store[id].district = null;
			} else {
				$scope.input.city = null;
				$scope.input.district = null;
				$scope.city_items = data;
				$scope.district_items = null;
			}

		};
		$scope.listen_city = function(data, id) {
			console.log(id);
			if(id != undefined)
				$scope.datas.store[id].district_items = data;
			else
				$scope.input.district = null;
		};
		//动态绑定三级地域的模型
		$scope.bind = function(ob1, ob2, ob3, i) {
			var items = new Array();
			var obj1 = {
				name: ob1,
				id: i
			};
			var obj2 = {
				name: ob2,
				id: i
			};
			var obj3 = {
				name: ob3,
				id: i
			};
			items.push(obj1);
			items.push(obj2);
			items.push(obj3);
			GoAddress.init_load($scope, items);
		}
		$scope.init_data();

		$scope.add_type = function() {
			if($scope.scope.id1 != undefined && $scope.scope.id1 != null) {
				$scope.cat_id = $scope.scope.id1;
				//等有了品类联动接口再替换成name
				$scope.cat_name = $scope.scope.id1;
				if($scope.scope.id2 != undefined && $scope.scope.id2 != null) {
					$scope.cat_id = $scope.scope.id1 + ',' + $scope.scope.id2;
					$scope.cat_name = $scope.scope.id1 + '-' + $scope.scope.id2;
					if($scope.scope.id3 != undefined && $scope.scope.id3 != null) {
						$scope.cat_id = $scope.scope.id1 + ',' + $scope.scope.id2 + ',' + $scope.scope.id3;
						$scope.cat_name = $scope.scope.id1 + '-' + $scope.scope.id2 + '-' + $scope.scope.id3;
					}
				}
			}
			var n = $scope.scope.cat_type.length;
			for(var i = 0; i < n; i++) {
				if($scope.scope.cat_type[i].id == $scope.cat_id)
					return;
			}
			if($scope.cat_id != null)
				$scope.scope.cat_type[n] = {
					"id": $scope.cat_id,
					"name": $scope.cat_name
				};
		}
		$scope.set_type = function() {
			var j = $scope.cat_type.length;

			if($scope.cat_type[$scope.scope.id1]) {

			} else {
				$scope.cat_type[$scope.scope.id1] = {
					"cat_id": $scope.scope.id1,
					"cat_name": null,
					"cat": new Array()
				};
			}
			if($scope.cat_type[$scope.scope.id1].cat[$scope.scope.id2]) {

			} else {
				$scope.cat_type[$scope.scope.id1].cat[$scope.scope.id2] = {
					"cat_id": $scope.scope.id2,
					"cat_name": null,
					"type": new Array()
				}
			}
			if($scope.cat_type[$scope.scope.id1].cat[$scope.scope.id2].type[$scope.scope.id3]) {

			} else {
				$scope.cat_type[$scope.scope.id1].cat[$scope.scope.id2].type[$scope.scope.id3] = {
					"cat_id": $scope.scope.id2,
					"cat_name": null,
					"type": new Array()
				}
			}


		}
		$scope.delete_type = function(index) {
			$scope.scope.cat_type.splice(index, 1);
		}
		$scope.get_discount = function() {
			if($scope.scope.order_type1 == true) {
				$scope.percent = null;
				$scope.total = $scope.scope.total;
				$scope.price = $scope.scope.price;
			} else if($scope.scope.order_type2 == true) {
				$scope.total = null;
				$scope.price = null;
				$scope.percent = $scope.scope.percent;
			}
		}
		$scope.getStore = function(store_id, Store) {
			if(store.length == 0) {
				store[0] = Store;
				store[0].checked = true;
				store[0].choose_goods = new Array();
			} else {
				for(var i = 0; i < store.length; i++) {
					if(store[i].store_id == store_id) {
						store.splice(i, 1);
						return;
					}
				}
				var n = store.length;
				store[n] = Store;
				store[n].checked = true;
				store[n].choose_goods = new Array();

			}
			$scope.store = store;

		}
		$scope.getGoods = function(goods_id, store_id, goods) {
			for(var i = 0; i < $scope.store.length; i++) {
				if($scope.store[i].store_id == store_id) {
					if($scope.store[i].choose_goods.length == 0) {
						$scope.store[i].choose_goods[0] = goods;
					} else {

						for(var n = 0; n < $scope.store[i].choose_goods.length; n++) {
							if($scope.store[i].choose_goods[n].goods_id == goods_id) {
								$scope.store[i].choose_goods.splice(n, 1);
								return;
							}

						}
						var j = $scope.store[i].choose_goods.length;
						$scope.store[i].choose_goods[j] = new Array();
						$scope.store[i].choose_goods[j] = goods;

					}

				}
			}

		}
		$scope.delete_store = function(store_id) {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].store_id == store_id) {
					$scope.datas.store[i].checked = false;
					if($scope.datas.store[i].goods != undefined)
						for(var n = 0; n < $scope.datas.store[i].goods.length; n++) {
							$scope.datas.store[i].goods[n].checked = false;
						}
						
					$scope.datas.store.splice(i, 1);
				}
			}
		}
		$scope.delete_goods = function(index, store_id) {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].store_id == store_id) {
					$scope.datas.store[i].goods.splice(index, 1);
					return;
				}

			}
		}

		$scope.region_choose = function(store_id) {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].store_id == store_id) {
					if($scope.datas.store[i].province != null) {
						if($scope.datas.store[i].region == undefined)
							$scope.datas.store[i].region = new Array();
						var n = $scope.datas.store[i].region.length;
						$scope.region_name = $scope.datas.store[i].province.name;
						$scope.region_id = $scope.region_name + ':' + $scope.datas.store[i].province.id;
						if($scope.datas.store[i].city != null) {
							$scope.region_name += '/' + $scope.datas.store[i].city.name;
							$scope.region_id = $scope.region_name + ':' + $scope.datas.store[i].city.id;
						}
						if($scope.datas.store[i].district != null) {
							$scope.region_name += '/' + $scope.datas.store[i].district.name;
							$scope.region_id = $scope.region_name + ':' + $scope.datas.store[i].district.id;
						}
						for(var n = 0; n < $scope.datas.store[i].region.length; n++) {
							if($scope.datas.store[i].region[n].region_id == $scope.region_id) {
								GoDialog.toast("该地域已存在");
								return;
							}

						}
						$scope.datas.store[i].region[n] = {
							"region_id": $scope.region_id,
							"region_name": $scope.region_name

						}
						return;
					}

				}
			}
		}
		$scope.region_delete = function(store_id, index) {
			for(var i = 0; i < $scope.datas.store.length; i++) {
				if($scope.datas.store[i].store_id == store_id) {
					$scope.datas.store[i].region.splice(index, 1);
					return;
				}
			}
		}

		$scope.get_storeinfo = function() {
			store_info = new Array();
			if($scope.datas.store == undefined) {
				GoDialog.toast("请选择活动代理商");
				return;
			} else if($scope.datas.store.length < 1) {
				GoDialog.toast("请选择活动代理商");
				return;
			} else {
				for(var i = 0; i < $scope.datas.store.length; i++) {
					region_info = new Array();
					goods_info = new Array();
					if($scope.datas.store[i].region == undefined) {
						GoDialog.toast("请添加" + $scope.datas.store[i].store_name + "的地域信息")
						return;
					} else if($scope.datas.store[i].region.length < 1) {
						GoDialog.toast("请添加" + $scope.datas.store[i].store_name + "的地域信息")
						return;
					}
					if($scope.datas.store[i].goods == undefined) {
						GoDialog.toast("请添加" + $scope.datas.store[i].store_name + "的商品信息")
						return;
					} else if($scope.datas.store[i].goods.length < 1) {
						GoDialog.toast("请添加" + $scope.datas.store[i].store_name + "的商品信息")
						return;
					}
					//获取地域信息
					if($scope.datas.store[i].region.length < 1) {
						GoDialog.toast("请选择" + $scope.datas.store[i].store_name + "的地域信息");
						return
					} else {
						for(var n = 0; n < $scope.datas.store[i].region.length; n++) {
							region_info[n] = {
								"region_id": $scope.datas.store[i].region[n].region_id
							}

						}

					}
					//获取商品信息
					for(var n = 0; n < $scope.datas.store[i].goods.length; n++) {
						if(!$scope.datas.store[i].goods[n].promotion_price) {
							GoDialog.toast("请输入" + $scope.datas.store[i].goods[n].goods_name + "的促销价");
							return
						} else if(!$scope.datas.store[i].goods[n].count) {
							GoDialog.toast("请输入" + $scope.datas.store[i].goods[n].goods_name + "数量");
							return
						} else if(!$scope.datas.store[i].goods[n].least_buy_num) {
							GoDialog.toast("请输入" + $scope.datas.store[i].goods[n].goods_name + "起够数");
							return
						} else
							goods_info[n] = {
								"goods_id": $scope.datas.store[i].goods[n].goods_id,
								"promotion_price": $scope.datas.store[i].goods[n].promotion_price,
								"count": $scope.datas.store[i].goods[n].count,
								"least_buy_num": $scope.datas.store[i].goods[n].least_buy_num
							}
					}
					if(goods_info.length > 0 && region_info.length > 0)
						store_info[i] = {
							"store_id": $scope.datas.store[i].store_id,
							"change_price": $scope.datas.store[i].change_price,
							"region": region_info,
							"goods": goods_info
						};
				}
				D_add.set_promotion($scope.act_id, $scope.scope.name, $scope.scope.start_time, $scope.scope.end_time, $scope.scope.create_group, $scope.scope.has_packets, $scope.v_type, $scope.scope.image, $scope.scope.brand_id, $scope.scope.cat_type, $scope.type, $scope.order_type, $scope.total, $scope.price, $scope.percent, store_info, function(data) {
					if(data.success == true) {
					} else {
					
					}
				});
			}

		}
		$scope.submit = function() {
			$scope.get_discount();
			if($scope.scope.name == undefined) {
				GoDialog.toast("请填写活动名称");
				return;
			}
			if($scope.scope.start_time == undefined) {
				GoDialog.toast("请填写活动开始时间");
				return;
			}
			if($scope.scope.end_time == undefined) {
				GoDialog.toast("请填写活动结束时间");
				return;
			}
			if($scope.scope.cat_type.length < 1) {
				GoDialog.toast("请选择活动的品类");
				return;
			}
			if($scope.type == 0) {
				GoDialog.toast("请选择活动的折扣类型");
				return;
			}
			$scope.get_storeinfo();

		}
		$scope.rule_choose = function(type) {
			if(type == 1) {
				$scope.p_type = 1;
			} else if(type == 2) {
				$scope.p_type = 2;
			} else if(type == 3) {
				$scope.p_type = 3;
			}
		}
		$scope.rule = function() {

		}
		$scope.get_rule = function() {
			rule = new Array();
			if($scope.p_type == 0) {
				GoDialog.toast("请选择红包类型");
			} else {
				if($scope.scope.type1 == true) {
					x = x + 1;
					if(!$scope.scope.count1) {
						GoDialog.toast("请输入商品数量");
						return;
					} else
						$scope.count = $scope.scope.count1;
					if(!$scope.scope.money1) {
						GoDialog.toast("请输入红包大小");
						return;
					} else
						$scope.money = $scope.scope.money1;
					if(!$scope.scope.packet_num1) {
						GoDialog.toast("请输入红包个数");
						return;
					} else
						$scope.packet_num = $scope.scope.packet_num1;
					if(!$scope.scope.issue_num1) {
						GoDialog.toast("请输入红包次数");
						return;
					} else
						$scope.issue_num = $scope.scope.issue_num1;
					$scope.rule_data(1);

				}
				if($scope.scope.type2 == true) {
					x = x + 1;
					if(!$scope.scope.count2) {
						GoDialog.toast("请输入订单数量");
						return;
					} else
						$scope.count = $scope.scope.count2;
					if(!$scope.scope.money2) {
						GoDialog.toast("请输入红包大小");
						return;
					} else
						$scope.money = $scope.scope.money2;
					if(!$scope.scope.packet_num2) {
						GoDialog.toast("请输入红包个数");
						return;
					} else
						$scope.packet_num = $scope.scope.packet_num2;
					if(!$scope.scope.issue_num2) {
						GoDialog.toast("请输入红包次数");
						return;
					} else
						$scope.issue_num = $scope.scope.issue_num2;
					$scope.rule_data(2);
				}
				if($scope.scope.type3 == true) {
					x = x + 1;
					if(!$scope.scope.count3) {
						GoDialog.toast("请输入成交额");
						return;
					} else
						$scope.count = $scope.scope.count3;
					if(!$scope.scope.money3) {
						GoDialog.toast("请输入红包大小");
						return;
					} else
						$scope.money = $scope.scope.money3;
					if(!$scope.scope.packet_num3) {
						GoDialog.toast("请输入红包个数");
						return;
					} else
						$scope.packet_num = $scope.scope.packet_num3;
					if(!$scope.scope.issue_num3) {
						GoDialog.toast("请输入红包次数");
						return;
					} else
						$scope.issue_num = $scope.scope.issue_num3;
					$scope.rule_data(3);
				}
			}
		}
		$scope.rule_data = function(type) {
			var n = rule.length;
			rule[n] = {
				"type": type,
				"count": $scope.count,
				"packet_num": $scope.packet_num,
				"issue_num": $scope.issue_num
			};
		}
		$scope.packets_edit = function() {
			$scope.get_rule();
			if(rule.length == x && x != 0) {
				D_add.packets_edit($scope.act_id, $scope.scope.flg, $scope.scope.advance, rule, function(data) {
					if(data.success == true) {
						GoDialog.toast("保存成功");

					} else
						GoDialog.toast("保存失败");
				})
			}
			rule = new Array();
			x = 0;
		}
		$scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
		$ionicModal.fromTemplateUrl('templates/modal/searchpro_agent.html', {
			scope: $scope,
		}).then(function(modal) {
			$scope.specModal = modal;
		});
		$scope.openSpec = function() {
			$scope.input.province = null; //省市区三级联动
			$scope.input.city = null;
			$scope.input.district = null;
			$scope.input.province_name = null; //省市区三级联动
			$scope.input.city_name = null;
			$scope.input.district_name = null;
			$scope.input.agent_count = 0;
			$scope.agenter_list = new Object();
			$scope.specModal.show();
		};
		$scope.closeSpec = function() {
			if($scope.datas.store == undefined)
				$scope.datas.store = new Array();
			if($scope.agenter_list != undefined) {

				for(var i = 0; i < $scope.agenter_list.store.length; i++) {
					if($scope.agenter_list.store[i].checked == true) {
						$scope.is_same = false;
						for(var n = 0; n < $scope.datas.store.length; n++) {
							if($scope.datas.store[n].store_id == $scope.agenter_list.store[i].store_id) {
								$scope.is_same = true;
								break;
							}

						};
						if($scope.is_same == false) {
							var n = $scope.datas.store.length;
							$scope.datas.store[n] = {
								"store_id": $scope.agenter_list.store[i].store_id,
								"store_name": $scope.agenter_list.store[i].store_name,
								"change_price": false,
								"province": null,
								"city": null

							};
							$scope.bind('datas.store[' + n + '].province', 'datas.store[' + n + '].city', 'datas.store[' + n + '].district', n);
						}

					}
				}
			}
			$scope.specModal.hide();
		};

		$ionicModal.fromTemplateUrl('templates/modal/searchpro_common.html', {
			scope: $scope,
		}).then(function(modal) {
			$scope.goodsModal = modal;
		});
		$scope.openGoods = function(store_id) {
			$scope.store_id = store_id;
			$scope.input.cat_type = null;
			$scope.input.name = null;
			$scope.input.brand_id = null;
			$scope.input.cat1 = null;
			$scope.input.cat2 = null;
			$scope.input.cat3 = null;
			$scope.input.goods_count = 0;
			$scope.goods_data = new Object();
			$scope.goodsModal.show();
		};
		$scope.closeGoods = function() {
			if($scope.datas.store != undefined && $scope.goods_data != undefined) {
				for(var n = 0; n < $scope.datas.store.length; n++) {
					if($scope.datas.store[n].store_id == $scope.store_id) {
						for(var i = 0; i < $scope.goods_data.goods.length; i++) {
							if($scope.goods_data.goods[i].checked == true) {
								$scope.goods_is_same = false;
								if($scope.datas.store[n].goods != undefined) {
									for(var j = 0; j < $scope.datas.store[n].goods.length; j++) {
										if($scope.datas.store[n].goods[j].goods_id == $scope.goods_data.goods[i].goods_id) {
											$scope.goods_is_same = true;
											break;
										}
									}
								} else
									$scope.datas.store[n].goods = new Array();
								if($scope.goods_is_same == false) {
									var m = $scope.datas.store[n].goods.length;
									$scope.datas.store[n].goods[m] = {
										"goods_id": $scope.goods_data.goods[i].goods_id,
										"goods_name": $scope.goods_data.goods[i].name,
										"image": $scope.goods_data.goods[i].image.image,
										"price": $scope.goods_data.goods[i].price,
										"promotion_price": $scope.goods_data.goods[i].preprice,
										"count": $scope.goods_data.goods[i].store,
										"least_buy_num": 1
									};

								}

							}
						}
					}
				}
			}
			$scope.goodsModal.hide();
		};
		//当我们用到模型时，清除它！
		$scope.$on('$destroy', function() {
			if($scope.specModal)
				$scope.specModal.remove();
			if($scope.goodsModal)
				$scope.goodsModal.remove();
		});
		//点击其他区域关闭弹出窗       
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {　
			console.log(event.target.nodeName);
			if(event.target.nodeName === 'ION-CONTENT'||event.target.nodeName ==='DIV') {　　　　
				if($scope.goodsModal) {　　　　　　
					$scope.goodsModal.hide();　　　　
				}
				if($scope.specModal) {　　　　　　
					$scope.specModal.hide();　　　　
				}　　　　　　　　　　　　　
			}
		});

		$scope.do_agentSearch = function() {
			if($scope.input.province != null) {
				$scope.input.region = $scope.input.province.id;
			}
			if($scope.input.city != null) {
				$scope.input.region += ',' + $scope.input.city.id;
			}
			if($scope.input.district != null) {
				$scope.input.region += ',' + $scope.input.district.id;
			}
			if($scope.input.key != null || $scope.input.region != null) {
				D_add.agenter_getlist($scope.input.key, $scope.input.region, function(data) {
					$scope.agenter_list = data;
					$scope.input.agent_count = $scope.agenter_list.page[0].count;
				})
			}

		}
		$scope.agenter_checked = function(store_id) {
			for(var i = 0; i < $scope.agenter_list.store.length; i++) {
				if($scope.agenter_list.store[i].store_id == store_id) {
					if($scope.agenter_list.store[i].checked == true)
						$scope.agenter_list.store[i].checked = false;
					else
						$scope.agenter_list.store[i].checked = true;
				}
			}
		}
		$scope.do_goodsSearch = function() {
			if($scope.input.cat1 != null) {
				$scope.input.cat_type = $scope.input.cat1;
			}
			if($scope.input.cat2 != null) {
				$scope.input.cat_type += ',' + $scope.input.cat2;
			}
			if($scope.input.cat3 != null) {
				$scope.input.cat_type += ',' + $scope.input.cat3;
			}
			if($scope.input.cat_type != null || $scope.input.name != null || $scope.input.brand_id != null) {
				D_add.goods_list($scope.input.cat_type, $scope.input.name, $scope.input.brand_id, function(data) {
					$scope.goods_data = data;
					$scope.input.goods_count = $scope.goods_data.page[0].count;
				})
			}
		}
		$scope.goods_checked = function(goods_id) {
			for(var i = 0; i < $scope.goods_data.goods.length; i++) {
				if($scope.goods_data.goods[i].goods_id == goods_id) {
					if($scope.goods_data.goods[i].checked == true) {
						$scope.goods_data.goods[i].checked = false;
					} else
						$scope.goods_data.goods[i].checked = true;

				}
			}

		}
	}

	controller.$inject = ['$scope', 'D_add', '$window', '$timeout', '$ionicSlideBoxDelegate', 'GoPage', 'GoDialog', '$stateParams', '$ionicPopup', 'GoAddress', '$ionicModal', 'GoPicture'];

	return app.registerController("d_addctrl",controller);
});